package com.burst.reptile.modular.zp.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.web.form.SortForm;
import com.burst.reptile.modular.zp.entity.ZpColumn;
import com.burst.reptile.modular.zp.mapper.ZpColumnMapper;
import com.burst.reptile.modular.zp.service.IZpColumnService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 栏目表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-15
 */
@Service
public class ZpColumnServiceImpl extends ServiceImpl<ZpColumnMapper, ZpColumn> implements IZpColumnService {

	@Override
	public List<ZpColumn> listAll() {
		return this.list(new QueryWrapper<ZpColumn>().orderByAsc("column_sort"));
	}
	
	@Override
	public List<ZpColumn> listByCloum(ZpColumn column) {
		QueryWrapper<ZpColumn> queryWrapper = new QueryWrapper<ZpColumn>();
		String columnName = column.getColumnName();
		if(StrUtil.isNotEmpty(columnName)) {
			queryWrapper.like("column_name", columnName);
		}
		queryWrapper.orderByAsc("column_sort");
		return this.list(queryWrapper);
	}

	@Override
	public String checkColumnKeyUnique(ZpColumn column) {
		Long columnId = ToolUtil.isEmpty(column.getColumnId()) ? -1L : column.getColumnId();
		ZpColumn info = this.baseMapper.selectOne(new QueryWrapper<ZpColumn>().eq("column_name", column.getColumnName()));
		if (ToolUtil.isNotEmpty(info) && info.getColumnId().longValue() != columnId.longValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	@Transactional
	public int insertColumn(ZpColumn column) {
		Integer columnSort = column.getColumnSort();
		Integer sort = columnSort == null || columnSort <= 0 ? 1 : columnSort;
		// 获取所有栏目
		List<ZpColumn> list = this.list(new QueryWrapper<ZpColumn>()
				.select("column_id", "column_sort")
				.orderByAsc("column_sort"));
		
		//重新排序
		if(ToolUtil.isEmpty(list)) {
			sort = 1;
		}else {
			if(sort > list.size()) {
				sort = list.size() + 1;
			}
		}
		column.setColumnSort(sort);
		list.add(sort - 1, column);
		int num = 0;
		for(int i = 0; i < list.size(); i++) {
			if(i == sort - 1) {
				num = this.baseMapper.insert(column);
			}else {
				ZpColumn zpColumn = list.get(i);
				zpColumn.setColumnSort(i + 1);
				num = this.baseMapper.updateById(column);
			}
		}
		return num;
	}

	@Override
	@Transactional
	public int updateColumn(ZpColumn column) {
		Integer columnId = column.getColumnId();
		if(ToolUtil.isEmpty(columnId)) {
			throw new CustomException("资源不存在");
		}
		Integer columnSort = column.getColumnSort();
		Integer sort = columnSort == null || columnSort <= 0 ? 1 : columnSort;
		ZpColumn byId = this.getById(columnId);
		if(byId == null) {
			throw new CustomException("资源不存在");
		}
		
		// 获取所有栏目
		List<ZpColumn> list = this.list(new QueryWrapper<ZpColumn>()
				.select("column_id", "column_sort")
				.ne("column_id", columnId)
				.orderByAsc("column_sort"));
		//重新排序
		if(ToolUtil.isEmpty(list)) {
			sort = 1;
		}else {
			if(sort > list.size()) {
				sort = list.size() + 1;
			}
		}
		byId.setColumnName(column.getColumnName());
		byId.setColumnSort(sort);
		list.add(sort - 1, byId);
		for (int i = 0; i < list.size(); i++) {
			ZpColumn zpColumn = list.get(i);
			zpColumn.setColumnSort(i + 1);
			this.baseMapper.updateById(zpColumn);
		}
		return 1;
	}

	@Override
	@Transactional
	public int deleteByIds(Integer[] ids) {
		return this.baseMapper.deleteColumnByIds(ids);
	}

	@Override
	public void sort(SortForm sortData) {
		Integer id = sortData.getId();
		Integer cSort = sortData.getSort();
		Integer sort = cSort == null || cSort <= 0 ? 1 : cSort;
		ZpColumn byId = this.getById(id);
		if(byId == null) {
			throw new CustomException("资源不存在");
		}
		Integer columnSort = byId.getColumnSort();
		if(sort == columnSort) {
			return;
		}
		
		// 获取所有栏目
		List<ZpColumn> list = this.list(new QueryWrapper<ZpColumn>()
				.select("column_id", "column_sort")
				.ne("column_id", id)
				.orderByAsc("column_sort"));
		//重新排序
		if(ToolUtil.isEmpty(list)) {
			sort = 1;
		}else {
			if(sort > list.size()) {
				sort = list.size() + 1;
			}
		}
		byId.setColumnSort(sort);
		list.add(sort - 1, byId);
		for (int i = 0; i < list.size(); i++) {
			ZpColumn zpColumn = list.get(i);
			zpColumn.setColumnSort(i + 1);
			this.baseMapper.updateById(zpColumn);
		}
	}

}
